<template>
  <BasicModal v-bind="$attrs" destroyOnClose title="预约处理" width="50%" @register="registerModal" @ok="handleSubmit">
    <Description class="mt-4" title="基本信息" :column="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }" :data="guestRecordInfo.data" :schema="guestrecordSchema" />
    <BasicForm @register="registerForm"></BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
import { reactive } from 'vue';
import { BasicModal, useModalInner } from '@apps/antd-admin/src/components/modal';
import { DescItem, Description } from '@apps/antd-admin/src/components/description';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/form';
import { getGuestrecord, checkSub } from '../data/api';
import { guestRecordCheckSchema } from '../data/data';

const emit = defineEmits(['success', 'register']);
let guestRecordInfo: Recordable = reactive({ data: {} });
const guestrecordSchema: DescItem[] = [
  {
    field: 'vipCardNo',
    label: '会员卡号',
    span: 3,
  },
  {
    field: 'appointmentName',
    label: '预约人',
    span: 3,
  },
  {
    field: 'appointmentPhone',
    label: '联系电话',
    span: 3,
  },
  {
    field: 'appointmentPhone',
    label: '联系电话',
    span: 3,
  },
  {
    field: 'numValue',
    label: '使用次数',
    span: 3,
  },
  {
    field: 'remark',
    label: '备注',
    span: 5,
  },
  {
    field: 'serverType',
    label: '服务类型',
    span: 3,
    render: (curVal, data) => {
      let serverTypeArray = [
        { label: '接机', value: 1 },
        { label: '送机', value: 2 },
      ];
      const serverType: any = serverTypeArray.find((item) => item.value == curVal);
      if (serverType) {
        return serverType.label;
      }
      return '';
    },
  },
  {
    field: 'flightNumber',
    label: '航班号',
    span: 3,
  },
  {
    field: 'originatingPlace',
    label: '始发地',
    span: 3,
  },
  {
    field: 'destinationPlace',
    label: '到达地',
    span: 3,
  },
  {
    field: 'enterpriseName',
    label: '企业名称',
    span: 3,
  },
  {
    field: 'appointmentDate',
    label: '预约时间',
    span: 3,
    render: (curVal, data) => {
      return curVal + ' ' + data.takeOffTime;
    },
  },
  {
    field: 'userName',
    label: '使用人',
    span: 3,
  },
  {
    field: 'appointmentState',
    label: '预约状态',
    span: 3,
    render: (curVal, data) => {
      let appointmentStateArray = [
        { label: '待处理', value: 0 },
        { label: '预约成功', value: 1 },
        { label: '预约失败', value: 2 },
      ];
      const appointmentState: any = appointmentStateArray.find((item) => item.value == curVal);
      if (appointmentState) {
        return appointmentState.label;
      }
      return '';
    },
  },
];

const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  labelWidth: 150,
  labelCol: { span: 4 },
  schemas: guestRecordCheckSchema,
  showActionButtonGroup: false,
});

//详情赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  let record = data.record;
  let guestrecordId = record.guestrecordId;
  getGuestrecord({
    guestrecordId: guestrecordId,
  }).then((result) => {
    console.info(result);
    guestRecordInfo.data = result;
    resetFields();
    let guestrecordData = {
      guestrecordId: result.guestrecordId,
    };
    setFieldsValue({
      ...guestrecordData,
    });
  });
});

async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    await checkSub(values);
    closeModal();
    emit('success', false);
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
